package app.beit.finportal.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

import app.beit.finportal.entity.Webboard;

public class WebboardDao extends BaseDao<Webboard>{
	
	@SuppressWarnings("unchecked")
	public List<Webboard> getAllPost() throws Exception{
		final Session session = super.sessionFactory.openSession();
		Query query = session.createQuery("from Webboard order by id desc");
		query.setFirstResult(0);
		query.setMaxResults(0);
		final List<Webboard> results = query.list();
		for(Webboard webboard : results){
			Hibernate.initialize(webboard.getListWebboardReply());
		}
		session.close();
		
		return results;
	}
	
	@SuppressWarnings("unchecked")
	public List<Webboard> getAllPostAtMainPage() throws Exception{
		final Session session = super.sessionFactory.openSession();
		Query query = session.createQuery("from Webboard order by id desc");
		query.setMaxResults(10);
		final List<Webboard> results = query.list();
		for(Webboard webboard : results){
			Hibernate.initialize(webboard.getListWebboardReply());
		}
		session.close();
		
		return results;
	}
	
	public Webboard getWebboardById(final String id) throws Exception{
		final Session session = super.sessionFactory.openSession();
		final Query query = session.createQuery("from Webboard where id=:webboard_id");
		query.setParameter("webboard_id", Long.parseLong(id));
		final Webboard result = (Webboard) query.uniqueResult();
		Hibernate.initialize(result.getListWebboardReply());
		session.close();
		return result;
	}
	
//	@SuppressWarnings("unchecked")
//	public List<Webboard> getAllReplyByWebboardId(final String id) throws Exception{
//		final Session session = super.sessionFactory.openSession();
//		final Query query = session.createQuery("from WebboardReply where webboard=:webboard_id");
//		query.setParameter("webboard_id", id);
//		final List<Webboard> results = query.list();
//		session.close();
//		return results;
//	}
}
